package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Note;
import cn.ztydata.teachingsystem.heaven.service.NoteService;
import cn.ztydata.teachingsystem.heaven.web.dto.NoteDto;
import cn.ztydata.teachingsystem.util.StringCodeUtil;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.MessageFormat;
import java.util.Map;

/**
 * 笔记控制器
 * Created by wyh on 2015/3/18.
 */
@Controller
@RequestMapping("/notes")
public class NoteController extends BaseController {
    @Autowired
    private NoteService noteService;
    @Autowired
    private WebContext webContext;

    /**
     * 笔记列表
     * @param groupId 群组ID
     * @param lessonId 课程ID
     * @param page 页码
     * @param pageSize 每页条数
     * @param tag 0全部 1我的 2收藏
     * @param keyword 关键字
     * @param chapterId 章ID
     * @param nodeId 节ID
     * @return JsonResultPage
     *
     * @author wyh
     * @since 2015-03-18
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage<NoteDto> getNoteList(@RequestParam("groupId") long groupId,
                                               @RequestParam("lessonId") long lessonId,
                                               @RequestParam("page") int page,
                                               @RequestParam("pageSize") int pageSize,
                                               @RequestParam("tag") int tag,
                                               @RequestParam(value = "keyword", required = false) String keyword,
                                               @RequestParam("chapterId") long chapterId,
                                               @RequestParam("nodeId") long nodeId){

        //校验参数
        Assert.isTrue(groupId >= 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.isTrue(lessonId >= 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程ID"));
        Assert.isTrue(page > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));
        Assert.isTrue(tag >= 0 && tag <= 2, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "tag"));
        Assert.isTrue(chapterId >= 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "章ID"));
        Assert.isTrue(nodeId >= 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "节ID"));

        //中文转码
        keyword = StringCodeUtil.urlParamTransCode(keyword, "utf-8");

        Page<Note> notePage = noteService.getNoteList(groupId, lessonId, page, pageSize, tag, keyword, chapterId, nodeId);

        log.debug("返回 笔记列表");
        return new JsonResultPage<>(new Page<>(notePage.getTotal(), notePage.getSize(), notePage.getNum(), mapper.mapList(notePage.getItems(), NoteDto.class)));
    }

    /**
     * 公开笔记
     * @param mapOpen
     * @return JsonResult
     *
     * @author wyh
     * @since 2015-3-19
     */
    @RequestMapping(value = "/open", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult openNote(@RequestBody Map<String, Object> mapOpen){
        //校验
        Assert.isTrue(Long.parseLong(mapOpen.get("id").toString()) > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "笔记ID"));
        Assert.isTrue((int)mapOpen.get("isOpen") == 0 || (int)mapOpen.get("isOpen") == 1, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "公开"));

        noteService.openNote(Long.parseLong(mapOpen.get("id").toString()), (int)mapOpen.get("isOpen"));

        log.debug("返回 公开笔记成功");
        return new JsonResult(true,"已更改");
    }

    /**
     * 收藏或取消收藏笔记
     * @param mapBest
     * @return JsonResult
     *
     * @author wyh
     * @since 2015-3-19
     */
    @RequestMapping(value = "/best", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult bestNote(@RequestBody Map<String, Object> mapBest){
        //校验
        Assert.isTrue(Long.parseLong(mapBest.get("id").toString()) > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "笔记ID"));
        Assert.isTrue((int)mapBest.get("best") == 0 || (int)mapBest.get("best") == 1, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "收藏"));

        noteService.bestNote(Long.parseLong(mapBest.get("id").toString()), (int)mapBest.get("best"));

        log.debug("返回 收藏笔记成功");
        return new JsonResult(true,"操作成功");
    }

    /**
     * 删除笔记
     * @param id 笔记ID
     * @return JsonResult
     *
     * @author wyh
     * @since 2015-3-20
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteNote(@PathVariable long id){
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "笔记ID"));

        noteService.deleteNote(id);

        log.debug("返回 删除笔记成功");
        return new JsonResult(true,"已删除");
    }

    /**
     * 笔记明细
     * @param id 笔记ID
     * @return JsonResultData
     *
     * @author wyh
     * @since 2015-3-20
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData<NoteDto> getNote(@PathVariable long id){
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "笔记ID"));

        log.debug("返回 笔记明细");
        return new JsonResultData<>(mapper.map(noteService.getNote(id), NoteDto.class));
    }

    /**
     * 新增笔记
     * @param noteDto 笔记
     * @return JsonResult
     *
     * @author wyh
     * @since 2015-3-20
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult saveNote(@RequestBody @Valid NoteDto noteDto){
        Assert.notNull(noteDto, "参数为空");

        noteService.saveNote(mapper.map(noteDto, Note.class));

        log.debug("返回 笔记新增成功");
        return new JsonResult(true, "已新增");
    }
}
